<template>
	<view class="box">
		<view class="info">
			<view class="tishi">
				请输入提现金额
			</view>
			<view class="content">
				<view class="l-pic">
					￥
				</view>
				<input class="price" :value="money" type="number" @input.prevent="handleInp" />
				<view class="r-all">
					全部提现
				</view>
			</view>
			<view class="bon">
				<view class="l">
					可提现金额：{{centerDetail.distribut_money_can}}元
				</view>
				<view class="r" @click="showRules = true">
					提现规则
				</view>
			</view>
			<view class="pic-btn" @click="handleSubmit">
				提现
			</view>
		</view>

		<view class="price-lis">
			<view class="title">
				提现记录
				<!-- （累计提现0.00元） -->
			</view>
			<view class="li-box">
				<view class="li" v-for="item in record" :key="item.id">
					<image :src="item.head_pic" class="img"></image>
					<view class="content">
						<view class="title">
							{{item.status_name}}
						</view>
						<view class="vtitle">
							{{item.add_at}}
						</view>
					</view>
					<view class="pric">
						{{item.real_fee}}元
					</view>
				</view>
				<view v-if="!record.length" style="text-align: center;">
					暂无记录
				</view>
			</view>
		</view>
		<!--  -->
		<u-modal :show="showRules" :closeOnClickOverlay="true" :showConfirmButton="false" @close="showRules=false"
			title="提现规则" width="690rpx">
			<view class="slot-content">
				<scroll-view class="rules-ctn" scroll-y="true">
					<rich-text :nodes="rules"></rich-text>
				</scroll-view>
				<view class="button" @click="showRules=false">
					确定
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		DistributionApi
	} from '@/api';
	export default {
		data() {
			return {
				showRules: false,
				rules: '', // 规则
				centerDetail: {
					distribut_money_can: 0, // 可提现金额
				}, // 活动中心
				record: [], // 提现记录
				money: 0, // 提现金额
			};
		},
		onLoad() {
			this.initData()
		},
		methods: {
			initData() {
				// 分销中心
				DistributionApi.distributeCenter()
					.then(res => {
						this.centerDetail = res.result
					})
					.catch(err => {
						this.$showToast(err || '加载金额失败')
					})
				DistributionApi.withdrawLog({
						size: 99999,
						p: 1
					})
					.then(res => {
						this.record = res.result.rows
					})
					.catch(err => {
						this.$showToast(err || '加载提现记录失败')
					})
				DistributionApi.getCommissionNote()
					.then(res => {
						this.rules = res.result.money
					})
					.catch(err => {
						this.$showToast(err || '加载活动说明失败')
					})
			},
			// 输入
			handleInp(e) {
				if (!Number(this.centerDetail.distribut_money_can)) {
					this.money = 0
					return this.$showToast('暂无可提现金额')
				}
				if (Number(e.detail.value) > Number(this.centerDetail.distribut_money_can)) {
					return this.money = Number(this.centerDetail.distribut_money_can)
				}
				return this.money = Number(e.detail.value)
			},
			// 确认提现
			handleSubmit() {
				if (!Number(this.centerDetail.distribut_money_can)) {
					return this.$showToast('暂无可提现金额')
				}
				uni.showModal({
					content: `您将可以提现${this.money}元，是否确认提现`,
					success: ({
						confirm
					}) => {
						if (!confirm)
							return
						DistributionApi.withdrawApply({
								withdraw_type: 4,
								fee: this.money,
							})
							.then(res => {
								this.$showToast('提现成功')
								this.initData()
							})
							.catch(err => {
								console.log('提现失败= ', err)
								this.$showToast('提现失败')
							})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		width: 100vw;
		padding: 30rpx;
		box-sizing: border-box;

		.button {
			margin-top: 50rpx;
			width: 613rpx;
			height: 94rpx;
			border-radius: 10rpx;
			opacity: 1;
			background: #fd8b07;
			text-align: center;
			line-height: 94rpx;
			color: #fff;
		}

		.price-lis {
			width: 690rpx;
			height: 774rpx;
			border-radius: 20rpx;
			opacity: 1;

			background: #ffffff;
			box-sizing: border-box;
			padding: 40rpx;

			.title {
				font-size: 32rpx;
			}

			.li-box {
				width: 100%;
				height: calc(100% - 100rpx);
				overflow-y: auto;
				margin-top: 40rpx;

				.li {
					width: 100%;
					height: 93rpx;
					opacity: 1;
					display: flex;
					align-items: center;
					margin-bottom: 60rpx;

					.pric {
						font-size: 38rpx;
						color: #ff0000;
					}

					.img {
						width: 93rpx;
						height: 93rpx;
						border-radius: 50%;
					}

					.content {
						flex: 1;
						margin-left: 20rpx;

						.title {
							font-size: 30rpx;
						}

						.vtitle {
							margin-top: 25rpx;
							font-size: 26rpx;
							color: #bbbbbb;
						}
					}
				}
			}

		}

		.info {
			width: 690rpx;
			height: 501rpx;
			border-radius: 20rpx;
			opacity: 1;
			background: #ffffff;
			box-sizing: border-box;
			padding: 50rpx 30rpx;

			.tishi {
				font-size: 30rpx;
				margin-bottom: 36rpx;
			}

			.pic-btn {
				width: 613rpx;
				height: 94rpx;
				border-radius: 10rpx;
				opacity: 1;
				background: #fd8b07;
				font-size: 32rpx;
				text-align: center;
				line-height: 94rpx;
				box-sizing: border-box;
				margin-top: 70rpx;
				color: #fff;
			}

			.bon {
				display: flex;
				align-items: center;
				width: 100%;
				justify-content: space-between;
				font-size: 28rpx;
				font-weight: 400;
				margin-top: 28rpx;

				.r {
					color: #fd8b07;
				}

				.l {
					color: #9b9b9b;
				}
			}

			.content {
				display: flex;
				align-items: center;
				width: 100%;
				height: 100rpx;
				box-sizing: border-box;
				padding: 16rpx 0;
				border-bottom: 1rpx solid #f2f2f2;

				.l-pic {
					font-size: 30rpx;
				}

				.price {
					flex: 1;
					color: #ff0000;
					// font-size: 68rpx;
					font-size: 60rpx;
				}

				.r-all {
					color: #9b9b9b;
					font-size: 28rpx;
				}
			}

		}
	}

	// 规则

	.rules-ctn {
		max-height: 500rpx;
	}
</style>